package com.whfc.wxmp.api.base.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.whfc.base.service.MessageService;
import com.whfc.common.result.ListData;
import com.whfc.emp.dto.AppEmpCorpDTO;
import com.whfc.emp.dto.AppGroupDTO;
import com.whfc.emp.dto.AppWorkTypeDTO;
import com.whfc.emp.dto.WxEmpDataCenterDTO;
import com.whfc.emp.service.AppEmpService;
import com.whfc.emp.service.AppEmpWarnService;
import com.whfc.emp.service.AppEmpWorkService;
import com.whfc.emp.service.AppGroupService;
import com.whfc.entity.dto.fvs.FvsInfoDTO;
import com.whfc.entity.dto.mach.MachInfoDTO;
import com.whfc.entity.dto.msg.AppMsgNumDTO;
import com.whfc.entity.dto.warn.WarnCountDTO;
import com.whfc.env.service.EnvWarnDataService;
import com.whfc.fim.service.FimResultService;
import com.whfc.fuum.dto.AppProjectDetailDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.service.AppProjectService;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.param.FvsDeviceListParam;
import com.whfc.fvs.service.FvsDeviceService;
import com.whfc.mach.dto.AppMachDTO;
import com.whfc.mach.param.MachQueryParam;
import com.whfc.mach.service.AppMachWarnService;
import com.whfc.safety.dto.SafetyCheckListDTO;
import com.whfc.safety.dto.SafetyIssueNumDTO;
import com.whfc.safety.service.SafetyCheckService;
import com.whfc.wxmp.api.base.service.ProjectService;
import com.whfc.wxmp.api.mach.v1.service.WxmpMachDataService;
import com.whfc.wxmp.api.safety.service.WxSafetyCheckService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author ：pyl
 * @date ：Created in 2022/7/18 18:02
 * @description：TODO
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @DubboReference(interfaceClass = AppProjectService.class, version = "1.0.0")
    private AppProjectService appProjectService;

    @DubboReference(interfaceClass = AppEmpWorkService.class, version = "1.0.0")
    private AppEmpWorkService appEmpWorkService;

    @DubboReference(interfaceClass = AppGroupService.class, version = "1.0.0")
    private AppGroupService appGroupService;

    @DubboReference(interfaceClass = AppEmpWarnService.class, version = "1.0.0")
    private AppEmpWarnService appEmpWarnService;

    @DubboReference(interfaceClass = EnvWarnDataService.class, version = "1.0.0")
    private EnvWarnDataService envWarnDataService;


    @DubboReference(interfaceClass = FimResultService.class, version = "1.0.0")
    private FimResultService fimResultService;

    @DubboReference(interfaceClass = AppMachWarnService.class, version = "1.0.0")
    private AppMachWarnService appMachWarnService;

    @DubboReference(interfaceClass = SafetyCheckService.class, version = "1.0.0")
    private SafetyCheckService safetyCheckService;

    @DubboReference(interfaceClass = FvsDeviceService.class, version = "1.0.0")
    private FvsDeviceService fvsDeviceService;

    @DubboReference(interfaceClass = MessageService.class, version = "1.0.0")
    private MessageService messageService;

    @Autowired
    private WxSafetyCheckService wxSafetyCheckService;
    @Autowired
    private WxmpMachDataService wxmpMachDataService;
    @DubboReference(interfaceClass = AppEmpService.class, version = "1.0.0")
    private AppEmpService appEmpService;

    @Override
    public AppProjectDetailDTO detail(Integer deptId) {
        List<AppWorkTypeDTO> appWorkTypeDTOS = appEmpWorkService.workTypeList(deptId);
        List<AppEmpCorpDTO> groupListByDeptId = appGroupService.groupListByDeptId(deptId, null);
        List<AppGroupDTO> departments = new ArrayList<>();
        groupListByDeptId.forEach(groups -> {
            List<AppGroupDTO> groupList = groups.getGroupList();
            departments.addAll(groupList);
        });
        AppProjectDetailDTO detail = appProjectService.detail(deptId);
        detail.setWorkTypes(appWorkTypeDTOS.size());
        detail.setDepartments(departments.size());
        WxEmpDataCenterDTO empDataCenter = appEmpService.getEmpDataCenter(deptId);
        detail.setEnterEmpNum(empDataCenter.getEnterEmpNum());
        return detail;
    }

    @Override
    public List<AppProjectDetailDTO> list(WxUser user) {
        String phone = user.getPhone();
        if (StrUtil.isBlank(phone)) return new ArrayList();
        List<AppProjectDetailDTO> list = appProjectService.listByWxUser(user);
        try{
            list.forEach(appProjectDetailDTO -> {
                AppMsgNumDTO unreadMsgNum = messageService.getUnreadMsgNum(user.getPhone(), appProjectDetailDTO.getDeptId());
                appProjectDetailDTO.setMessageTotal(unreadMsgNum.getTotal());
            });
        }catch (Exception e){
        }
        return list;
    }

    @Override
    public WarnCountDTO warnList(Integer deptId) {
        DateTime now = DateUtil.date();
        Date dateTimeStart = com.whfc.common.util.DateUtil.getDateBegin(now);
        Date dateTimeEnd = com.whfc.common.util.DateUtil.getDateEnd(now);
        Integer warnNumEmp = appEmpWarnService.getWarnNum(Arrays.asList(deptId), dateTimeStart, dateTimeEnd);
        Integer warnNumEnv = envWarnDataService.getWarnNum(Arrays.asList(deptId), null, dateTimeStart, dateTimeEnd);
        Integer warnNumFim = fimResultService.warnNumstatic(deptId, null, dateTimeStart, dateTimeEnd).stream().mapToInt(a->a.getCount()).sum();
        Integer warnNumMach = appMachWarnService.warnNumStatic(deptId, null, dateTimeStart, dateTimeEnd).getWarnNum();
        return WarnCountDTO.builder().warnNumEmp(warnNumEmp).warnNumEnv(warnNumEnv).warnNumFim(warnNumFim)
                .warnNumTotal(warnNumMach+warnNumFim+warnNumEnv+warnNumEmp)
                .warnNumMach(warnNumMach).build();
    }

    @Override
    public SafetyCheckListDTO safeList(Integer deptId) {
        List<SafetyIssueNumDTO> safetyIssueNumDTOS = safetyCheckService.safetyLogGroupByIssueId(deptId);

//        SafetyCheckListDTO checkListByDeptId = wxSafetyCheckService.getCheckListByDeptId(deptId);

        SafetyCheckListDTO checkListByDeptId = new SafetyCheckListDTO();
        int sum = safetyIssueNumDTOS.stream().mapToInt(SafetyIssueNumDTO::getNum).sum();
        checkListByDeptId.setSafetyIssueNumDTOList(safetyIssueNumDTOS);
        checkListByDeptId.setAllNum(sum);
        return checkListByDeptId;
    }

    @Override
    public MachInfoDTO machInfo(Integer deptId) {
        MachQueryParam machQueryParam = new MachQueryParam();
        machQueryParam.setDeptId(deptId);
        ListData<AppMachDTO> list = wxmpMachDataService.list(machQueryParam);
        List<AppMachDTO> list1 = list.getList();
        Integer warnNumMach = appMachWarnService.warnNumStatic(deptId, null, null, null).getWarnNum();

        return MachInfoDTO.builder().machCount(list1.size()).warnTotalCount(warnNumMach).build();
    }
    @Override
    public FvsInfoDTO fvsInfo(Integer deptId) {
        FvsDeviceListParam fvsDeviceListParam = new FvsDeviceListParam();
        fvsDeviceListParam.setDeptId(deptId);
        ListData<FvsDeviceDTO> listData = fvsDeviceService.listData(fvsDeviceListParam);
        List<FvsDeviceDTO> list = listData.getList();
        Integer indexOnline = 0;
        for (int i = 0; i < list.size(); i++) {
            FvsDeviceDTO fvsDeviceDTO =  list.get(i);
            //设备状态:0-离线 1-在线
            Integer deviceStatus = fvsDeviceDTO.getDeviceStatus();
            if(deviceStatus.equals(1))indexOnline++;
        }
        BigDecimal div = new BigDecimal(0.00);
        if(!indexOnline.equals(0)){
            div = NumberUtil.round(NumberUtil.div((float) indexOnline, list.size())*100,2);
        }
        return FvsInfoDTO.builder().fvsCount(list.size()).onlineCount(indexOnline).onlinePercent(div.doubleValue()).build();
    }

}
